//
//  TextFieldViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TextFieldViewController.h"
#import "TextFileTableCell.h"
#import "ZHDataService.h"
#import "ChangeInfoViewController.h"

@implementation TextFieldViewController

@synthesize field,myTableView,fieldType;

#pragma mark - AbstractTabViewController methods
- (void)rightItemClicked
{
    NSUInteger count = [[self.navigationController viewControllers] count];
    ChangeInfoViewController *changeController = (ChangeInfoViewController *)[[self.navigationController viewControllers] objectAtIndex:count-2];
    switch (self.fieldType) {
            case kUserName:
            changeController.changeUser.userName = self.field.text;
            break;
        case kOldPassword:
            changeController.changeUser.userPwd = self.field.text;
            break;
        case kEmail:
            changeController.changeUser.userEmail = self.field.text;
            break;
        case kNewPassword:
            changeController.changedPwd = self.field.text;
        default:
            break;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - View lifecycle

- (void)dealloc
{
    [myTableView release];
    [field release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    TextFileTableCell *cell = (TextFileTableCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
    self.field = cell.textField;
    NSUInteger count = [[self.navigationController viewControllers] count];
    ChangeInfoViewController *changeController = (ChangeInfoViewController *)[[self.navigationController viewControllers] objectAtIndex:count-2];
    if(self.fieldType == kUserName)
        self.field.text = changeController.changeUser.userName;
    else if(self.fieldType == kEmail)
        self.field.text = changeController.changeUser.userEmail;
    else if(self.fieldType == kOldPassword)
    {
        self.field.secureTextEntry = YES;
        self.field.text = changeController.changeUser.userPwd;
    }
    else
    {
        self.field.secureTextEntry = YES;
        self.field.text = changeController.changedPwd;
    }
    [self.field becomeFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"输入内容";
    [self addNaviBarRightItemWithTitle:@"完成"];
    NSUInteger count = [[self.navigationController viewControllers] count];
    ChangeInfoViewController *changeController = (ChangeInfoViewController *)[[self.navigationController viewControllers] objectAtIndex:count-2];
    if(changeController.viewType == kViewEdit)
        [self setBackgroundImageViewForTabView];
    else
        [self setBackgroundImageViewForRegisterView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.myTableView = nil;
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"请输入需要修改的内容";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"点击完成确认修改";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    TextFileTableCell *cell = (TextFileTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if(cell == nil)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"TextFileTableCell" owner:self options:nil];
        cell = (TextFileTableCell *)[nibs lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

@end
